<template>
  <div class="advertisingList pd-20">
    <el-select v-model="hospitalId"
               clearable
               filterable
               remote
               reserve-keyword
               placeholder="请输入关键词"
               :remote-method="remoteMethod"
               :loading="loading"
               class="w-200">
      <el-option v-for="item in options"
                 :key="item.id"
                 :label="item.name"
                 :value="item.id">
      </el-option>
    </el-select>
    <el-button @click="handleQuery"
               type="primary">查 询</el-button>
    <el-button @click="handleAdd"
               type="primary">新 增</el-button>
    <el-table stripe
              border
              :data="tableData"
              style="width: 100%"
              class="mt-20">
      <el-table-column align="center"
                       type="index"
                       min-width="20">
      </el-table-column>
      <el-table-column align="center"
                       prop="title"
                       label="广告标题"
                       min-width="100">
      </el-table-column>
      <el-table-column align="center"
                       prop="hospitalName"
                       label="医院名称"
                       min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.hospitalDto != ''">{{scope.row.hospitalDto.name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       prop="advertising"
                       label="广告语"
                       min-width="300">
      </el-table-column>
      <el-table-column align="center"
                       label="广告图片"
                       min-width="100">
        <template slot-scope="scope">
          <el-image :src="scope.row.logoUrl"
                    :preview-src-list="[scope.row.logoUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center"
                       prop="createTime"
                       label="发布时间"
                       min-width="120"
                       :formatter="formatterDate">
      </el-table-column>
      <el-table-column align="center"
                       min-width="100"
                       label="操作">
        <template slot-scope="scope">
          <el-button size="mini"
                     @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <page @getList="getList"
          :pageTotal="pageTotal"
          ref="page"></page>
    <el-dialog :show-close="false"
               title="新增/编辑"
               :visible.sync="dialogVisible"
               width="30%">
      <el-form :rules="rules"
               ref="form"
               :model="form"
               label-width="80px">
        <el-form-item label="医院名称"
                      prop="hospitalId">
          <el-select v-model="form.hospitalId"
                     clearable
                     filterable
                     remote
                     reserve-keyword
                     placeholder="请输入医院名称..."
                     :remote-method="remoteMethod"
                     :loading="loading">
            <el-option v-for="item in options"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告标题"
                      prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="广告语"
                      prop="advertising">
          <el-input v-model="form.advertising"></el-input>
        </el-form-item>
        <el-form-item label="广告图片"
                      prop="logoUrl">
          <el-upload class="avatar-uploader"
                     :action="uploadImgUrl"
                     :show-file-list="false"
                     :on-success="handleAvatarSuccess"
                     :before-upload="beforeAvatarUpload">
            <img v-if="form.logoUrl"
                 :src="form.logoUrl"
                 class="avatar">
            <i v-else
               class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary"
                   @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Page from '@/base/page/page'
  import { mapGetters } from 'vuex'
  export default {
    name: "advertisingList",
    data () {
      return {
        loading: false,
        tableData: [],
        dialogVisible: false,
        options: [],
        hospitalId: '',
        pageTotal: 0,
        form: {
          id: '',
          title: '',
          hospitalId: '',
          advertising: '',
          logoUrl: ''
        },
        uploadImgUrl: this.api.file.uploadImage,
        rules: {
          hospitalId: [{ required: true, message: '医院不能为空', trigger: 'blur' }],
          title: [{ required: true, message: '广告标题不能为空', trigger: 'blur' }],
          advertising: [{ required: true, message: '广告语不能为空', trigger: 'blur' }],
          logoUrl: [{ required: true, message: '广告图片不能为空', trigger: 'blur' }]
        },
      }
    },
    watch: {
      dialogVisible: function () {
        if (!this.dialogVisible) {
          this.$nextTick(() => {
            this.form = {
              id: '',
              title: '',
              hospitalId: '',
              advertising: '',
              logoUrl: ''
            }
          })
        }
      }
    },
    mounted () {
      this.getList()
    },
    methods: {
      getList () {
        this.post(this.api.advertising.selectPageAll, { "pageSize": 20, "pageStart": 0, "hospitalId": this.hospitalId }).then((res) => {
          if (res.data.code === '200') {
            this.tableData = res.data.object;
            this.pageTotal = res.data.totalNum
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.loading = true;
          this.post(this.api.searchHospital, { "pageSize": 20, "pageStart": 0, "name": query }).then((res) => {
            if (res.data.code === '200') {
              this.options = res.data.object;
            }
          });
          this.loading = false
        }
      },
      handleQuery () {
        this.getList()
      },
      handleAdd () {
        this.dialogVisible = true
      },
      formatterDate (row) {
        return this.commonFunction.timestampFormat(new Date(row.createTime), 'yyyy-MM-dd hh:mm:ss')
      },
      handleEdit (row) {
        this.dialogVisible = true;
        this.form = row;
        this.$nextTick(() => {
          this.options = [{
            id: row.hospitalDto.id,
            name: row.hospitalDto.name
          }];
        })
      },
      handleDel (row) {
        this.$Modal.confirm({
          title: '确认',
          content: '<p>您确认删除该数据？</p>',
          onOk: () => {
            let param = {
              id: row.id
            };
            this.post(this.api.advertising.delete, param).then(res => {
              if (res.data.code === '200') {
                this.$message({
                  showClose: true,
                  message: "删除成功",
                  type: 'success'
                });
              }
              this.getList()
            })
          },
          error: (data) => {
            this.$Message.error(data.message);
          },
          onCancel: () => { }
        });
      },
      handleSave () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.post(this.api.advertising.save, this.form).then((res) => {
              if (res.data.code === '200') {
                this.$message({
                  showClose: true,
                  message: "保存成功",
                  type: 'success'
                });
                this.dialogVisible = false;
                this.getList()
              } else {
                this.$message({
                  showClose: true,
                  message: res.data.message,
                  type: 'error'
                })
              }
            })
          }
        })
      },
      handleAvatarSuccess (res) {
        this.form.logoUrl = res.url
      },
      beforeAvatarUpload (file) {
        let types = ['image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/bmp']
        const isJPG = types.includes(file.type);
        const isLt10M = file.size / 1024 / 1024 < 10;

        /*if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }*/
        if (!isLt10M) {
          this.$message.error('上传头像图片大小不能超过 10MB!');
        }
        return isLt10M;
      },
      cancel () {
        this.dialogVisible = false;
      }
    },
    computed: {
      ...mapGetters([
        'page'
      ])
    },
    components: {
      Page,
    }
  }
</script>

<style scoped>
  .avatar-uploader {
    width: 178px;
    height: 178px;
    border: 1px dashed #606266;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
